<template>
    <div id="signOn">
        <div id="separator">LOGIN</div>
        <div id="login">
            <template v-if="throughPhone">
                <div class="inputLine">
                    <div class="label">Phone:</div>
                    <input type="text" name="code" v-model="phone" />
                    <div id="submitButton" class="verify" @click="verify">{{ send ? `${time}s` :'Send Code'}}</div>
                </div>

                <div class="inputLine">
                    <div class="label">VerifyCode:</div>
                    <input type="text" name="phone" v-model="verifyCode" />
                </div>

                <div class="inputLine">
                    <div class="label">Username:</div>
                    <input type="text" name="username" v-model="username" />
                </div>
            </template>

            <template v-else>
                <div class="inputLine">
                    <div class="label">Username:</div>
                    <input type="text" name="username" v-model="username" />
                </div>
                <div class="inputLine">
                    <div class="label">Password:</div>
                    <input type="password" name="username" v-model="password" />
                </div>
                <div class="inputLine">
                    <div class="label">Info:</div>
                    <input type="text" name="info" disabled value="This is something of no use" style="font-size: 1.2rem; top: 18px; border-bottom: none" />
                </div>
            </template>

            <div class="inputLine" id="errorMessage">{{ errorMessage }}</div>

            <div class="inputLine" id="submitLine">
                <div id="submitButton" @click="handleSignOn">Sign In</div>
            </div>

            <div class="inputLine" id="registerLine">
                <nuxt-link id="registerButton" to="/userRegister">Register Now!</nuxt-link>
                <div id="changeButton" @click="throughPhone = !throughPhone">Through {{ throughPhone ? 'password' : 'phone' }}</div>
            </div>
        </div>
    </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
    data() {
        return {
            username: '',
            phone: '',
            verifyCode: '',
            errorMessage: '',
            send: false,
            time: 60,
            timer: null,
            throughPhone: true,
            password: ''
        }
    },
    methods: {
        ...mapActions({
            signOn: 'account/signOn',
            getCode: 'account/getCode',
            logIn: 'account/logIn'
        }),
        handleSignOn() {
            let self = this
            if(this.throughPhone) {
                if(this.username === '' || this.verifyCode === ''){
                    this.errorMessage = '用户名或验证码不能为空'
                    return
                }
                this.signOn({username: this.username, phone: this.phone, verifyCode: this.verifyCode}).then((res) => {
                    this.$router.back(1)
                }, (error) => {
                    self.errorMessage = error
                })
            }
            else {
                if(this.username === '' || this.password === ''){
                    this.errorMessage = '用户名或密码不能为空'
                    return
                }
                this.logIn({username: this.username, password: this.password}).then((res) => {
                    this.$router.back(1)
                }, (error) => {
                    self.errorMessage = error
                })
            }
        },
        verify() {
            if(this.send) {
                return
            }
            if(this.phone === '') {
                this.errorMessage = '请填写手机号'
                return
            }
            this.getCode(this.phone).then((res) => {
                this.send = true
                let self = this
                this.timer = setInterval(() => {
                    self.time--
                }, 1000)
            }, (error) => {
                this.errorMessage = error
            })
        },
        destroyed() {
            clearInterval(this.timer)
        },
    },
    watch: {
        time(newVal, oldVal) {
            if(newVal <= 0) {
                clearInterval(this.timer)
                this.send = false
                this.time = 60
            }
        }
    }
}
</script>

<style lang="scss" scoped>
@keyframes showSignOn
{
    0%
    {
        opacity: 0;
        transform: translateY(-10px);
    }
    100%
    {
        opacity: 1;
        transform: translateY(0px);
    }
}

#signOn
{
    animation: showSignOn 0.8s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards;
    color: #833d1e;
    width: 45%;
    margin: 70px auto;
    height: 621px;
    border-radius: 3px;
    background-color: #efe7db;
    box-shadow: 2px 2px 15px #78a686;
    padding: 1px;
    overflow: hidden;
}

#login
{
    width: 100%;
    float: left;
    height: 400px;
}

#signOn>#separator
{
    border: none;
    border-bottom: 3px solid #833d1e;
    text-align: center;
    overflow: hidden;
    width: 20%;
    margin: 35px auto 0 auto;
    font-size: 2em;
    color: #833d1e;
}

.inputLine
{
    width: 100%;
    height: 50px;
    overflow: hidden;
    margin-top: 55px;
    margin-left: 0;
    margin-right: 0;
}

.inputLine>.label
{
    float: left;
    position: relative;
    height: 26px;
    top: 24px;
    font-size: 1.3em;
    width: 18%;
    margin: 0 0 0 20%;
}

#verifyCode
{
    width: 60px;
    position: relative;
    float: left;
    top: 20px;
    height: 30px;
    padding: 0;
}

#verifyCode>img
{
    height: 30px;
}

.inputLine>input
{
    outline: none;
    width: 40%;
    border: none;
    background: transparent;
    color: #833d1e;
    border-bottom: 3px solid #833d1e;
    height: 30px;
    position: relative;
    top: 15px;
    font-size: 1.1em;
    float: left;
}

#verifyCodeInput
{
    width: 25%;
    margin-right: 5%;
}

#submitLine
{
    margin-top: 40px;
}

#submitButton
{
    cursor: pointer;
    text-align: center;
    line-height: 35px;
    transition: all 0.3s cubic-bezier(0.55, 0.085, 0.68, 0.53);
    border: 3px solid #507e64;
    color: #507e64;
    width: 250px;
    height: 40px;
    margin: 4px auto;
    border-radius: 8px;
    font-size: 1.1em;
}

#submitButton:hover
{
    background-color: #507e64;
    color: #efe7db;
}

@keyframes errorButton1
{
    0% {}
    100%
    {
        background-color: #833d1e;
        border: 3px solid #833d1e;
        color: #efe7db;
    }
}

@keyframes errorButton2
{
    0%
    {
        transform: translateX(0);
    }
    20%
    {
        transform: translateX(10px);
    }
    40%
    {
        transform: translateX(-10px);
    }
    60%
    {
        transform: translateX(10px);
    }
    80%
    {
        transform: translateX(-10px);
    }
    100%
    {
        transform: translateX(0);
    }
}

@keyframes errorButton3
{
    0%
    {
        background-color: #833d1e;
        color: #efe7db;
    }
    100%
    {
        background-color: transparent;
        border: 3px solid #507e64;
        color: #507e64;
    }
}

.errorButton
{
    animation: errorButton1 0.2s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards, errorButton2 0.4s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards 0.4s, errorButton3 0.2s cubic-bezier(0.55, 0.085, 0.68, 0.53) forwards 0.8s;
}

#errorMessage
{
    transition: all 0.2s cubic-bezier(0.55, 0.085, 0.68, 0.53);
    font-size: 1.2em;
}

#errorMessage
{
    text-align: center;
    height: 25px;
    margin: 5px 0 0 0;
}

#registerLine
{
    margin-top: 8px;
}

#registerButton
{
    float: right;
    height: 35px;
    line-height: 35px;
    margin: 7px 0;
    cursor: pointer;
    width: 90px;
    text-align: center;
    overflow: hidden;
    position: relative;
    right: 50px;
    font-size: 1em;
    color: #833d1e;
}

#registerButton::before, #registerButton::after, #changeButton::before, #changeButton::after
{
    content: '';
    transition: width 0.2s cubic-bezier(0.55, 0.085, 0.68, 0.53);
    position: absolute;
    top: 0;
    height: 90%;
    border-bottom: 3px solid #833d1e;
    width: 0;
}

#changeButton
{
    float: right;
    height: 35px;
    line-height: 35px;
    margin: 7px 0;
    cursor: pointer;
    width: 120px;
    text-align: center;
    overflow: hidden;
    position: relative;
    right: 80px;
    font-size: 1em;
}

#registerButton::before, #changeButton::before
{
    right: 50%;
}

#registerButton::after, #changeButton::after
{
    left: 50%;
}

#registerButton:hover::before, #registerButton:hover::after, #changeButton:hover::before, #changeButton:hover::after
{
    width: 50%;
}

.verify#submitButton {
    position: absolute;
    right: 30px;
    width: 100px;
}
</style>